package net.sansi.v3correctserver.constenum;

import javax.servlet.http.HttpServletRequest;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.Map;

/**
 * Created by Fang
 * create time : 2025/2/18
 * IDE : IntelliJ IDEA
 * <p>
 * 线程局部变量，用于存储一些线程私有的变量
 */

public class ThreadLocalValues {

    /**
     * 私有构造方法
     * 该类是单例模式，不允许外部创建实例
     */
    private ThreadLocalValues() {}

    // 单例模式
    public final static ThreadLocalValues instance = new ThreadLocalValues();

    // 存储请求头
    public final ThreadLocal<Map<String, String>> requestHeaders = new ThreadLocal<>();

    /**
     * 设置请求头
     *
     * @param request 请求
     */
    public void setRequestHeaders(HttpServletRequest request) {
        Map<String, String> map = new HashMap<>();
        Enumeration<String> headerNames = request.getHeaderNames();
        while (headerNames.hasMoreElements()) {
            String key = headerNames.nextElement();
            String value = request.getHeader(key);
            map.put(key, value);
        }
        setRequestHeaders(map);
    }

    /**
     * 设置请求头
     *
     * @param headers 请求头
     */
    public void setRequestHeaders(Map<String, String> headers) {
        requestHeaders.set(headers);
    }
}
